import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

public class NoticeCrawler {
    public static void main(String[] args) {
        String url = "https://www.njpta.org.cn/newsCenter/33.html";

        try {
            System.out.println("开始请求URL: " + url);

            // 发送HTTP GET请求
            HttpResponse response = HttpRequest.get(url)
                    .timeout(30000)
                    .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")
                    .execute();


            if (response.isOk()) {
                String html = response.body();

                Document doc = Jsoup.parse(html);

                // 多种选择器尝试
                List<String> selectors = List.of(
                        "ul.infos.newsCenter li",
                        ".infos.newsCenter li",
                        ".newsCenter li",
                        ".infos ul li",
                        "ul li[style*='padding: 12px 0']"
                );

                Elements noticeItems = null;
                String usedSelector = "";

                for (String selector : selectors) {
                    noticeItems = doc.select(selector);
                    if (!noticeItems.isEmpty()) {
                        usedSelector = selector;
                        break;
                    }
                }

                if (noticeItems == null || noticeItems.isEmpty()) {
                    System.out.println("所有选择器都未找到公告项，开始分析页面结构...");

                    // 分析页面结构
                    System.out.println("页面标题: " + doc.title());

                    // 查找所有ul元素
                    Elements allUls = doc.select("ul");
                    System.out.println("页面中共有 " + allUls.size() + " 个ul元素");

                    for (int i = 0; i < allUls.size(); i++) {
                        Element ul = allUls.get(i);
                        Elements lis = ul.select("li");
                    }

                    return;
                }

                List<String> targetNotices = new ArrayList<>();

                for (int i = 0; i < noticeItems.size(); i++) {
                    Element item = noticeItems.get(i);

                    // 多种方式提取发布日期
                    String publishDate = "";

                    // 方式1: 从remarkRT中的a标签提取
                    Element dateElement = item.selectFirst("div.remarkRT a");
                    if (dateElement != null) {
                        publishDate = dateElement.text().trim();
                    }

                    // 提取标题
                    String title = "";
                    Element titleElement = item.selectFirst("div.infosTitle");
                    if (titleElement != null) {
                        title = titleElement.ownText().trim();
                    } else {
                        // 尝试其他方式找标题
                        titleElement = item.selectFirst("a[title]");
                        if (titleElement != null) {
                            title = titleElement.attr("title");
                        }
                    }

                    // 检查是否为2025-11-11
                    if ("2025-11-11".equals(publishDate)) {
                        targetNotices.add("发布日期: " + publishDate + " - 标题: " + title);
                    }
                }

                // 输出结果
                if (!targetNotices.isEmpty()) {
                    System.out.println("找到 " + targetNotices.size() + " 条发布日期为2025-11-11的公告:");
                    for (String notice : targetNotices) {
                        System.out.println(notice);
                    }
                }
            }
        } catch (Exception e) {
            System.out.println("请求过程中发生错误: " + e.getMessage());
            e.printStackTrace();
        }
    }
}